package com.rivulet.ncards;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;

import static android.os.Build.VERSION.SDK_INT;

public class HelloActivity extends AppCompatActivity {
    TextView show_message;
    EditText message;
    Button send,back;
    static String msg="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hello);
        show_message=(TextView) findViewById(R.id.show_message);
        message=(EditText) findViewById(R.id.message);
        send=(Button) findViewById(R.id.send_message);
        back=(Button) findViewById(R.id.hello_back);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    while (true){
                        URL url=new URL("http://81.69.228.75:8097/get");
                        HttpURLConnection http=(HttpURLConnection) url.openConnection();
                        http.setRequestMethod("GET");
                        http.connect();
                        if(http.getResponseCode() == HttpURLConnection.HTTP_OK){
                            //得到响应流
                            InputStream inputStream = http.getInputStream();
                            //将响应流转换成字符串
                            byte[] data=new byte[1024];
                            inputStream.read(data);
                            String result = new String(data,"utf-8");//将流转换为字符串。
                            show_message.setText(result);
                            inputStream.close();
                            Thread.sleep(500);
                        }
                        http.disconnect();
                    }
                }catch (Exception e){
                    System.out.println(e.toString());
                }
            }
        }).start();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable(){
                    @Override
                    public void run() {
                        try {
                            URL url=new URL("http://81.69.228.75:8097/post?postMessage="+message.getText().toString());
                            HttpURLConnection http=(HttpURLConnection) url.openConnection();

                            http.setRequestMethod("GET");
                            http.setConnectTimeout(10000);
                            http.setReadTimeout(10000);

                            http.connect();
                            int responseCode = http.getResponseCode();
                            if(responseCode == HttpURLConnection.HTTP_OK){
                                //得到响应流
                                message.setText("");
                            }
                        }catch (Exception e){
                            System.out.println(e.toString());
                        }
                    }
                }).start();
            }
        });
    }
}

